<?php
namespace app\home\controller;
use \think\Cookie;
use think\Db;
use \think\View;
use \think\Model;
use \think\Request;
use \GatewayClient\Gateway;
class User extends Model
{
    protected $resultSetType = 'collection';
}
class StatusData extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = 'time';
    protected $updateTime = false;
}
class Url extends Model
{
    protected $resultSetType = 'collection';

}
class Urladmin extends \think\Controller
{
    public function url()      //站点列表
    {
        $person = new User();
        $cookie = Cookie::get('safecode');
        $person_data = User::getBySafecode($cookie);
        $uid = $person_data['Id'];
        $url = new Url();
        $url_list = Url::where('uid', $uid)->select()->toArray();
        $view = new View();
        $view->assign('user', $person_data);
        $view->assign('url', $url_list);
        return $view->fetch('url', [], ['__PUBLIC__' => '/public/static']);

    }
    public function admin()
    {
        $url = input('post.url');
        $domain = $url;
        $cookie = Cookie::get('safecode');
        $person = User::getBySafecode($cookie)->toArray();
        $url_data = Url::getByDomain($url)->toArray();
        if ($url_data['uid'] == $person['Id']) {
            if (input('post.do') == '1') {
                $url1 = $url . '/index.php/api/api/api?copy=' . input('post.copy') . '&op=update_data&identity_code=' . $url_data['identity_code'] . '&name=' . input('post.name');
                $ch = curl_init();
                $timeout = 5;
                curl_setopt($ch, CURLOPT_URL, $url1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);   //参数设置
                $return1 = curl_exec($ch);  //返回数据
                curl_close($ch);
                $return_data1 = json_decode($return1, true);
                if ($return_data1['message'] == 'success') {
                    $this->success('操作成功', 'url');
                } else {
                    $this->error('操作失败');
                }
            }
            $url = $url . '/index.php/api/api/api?op=select&identity_code=' . $url_data['identity_code'];
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);   //参数设置
            $return = curl_exec($ch);  //返回数据
            curl_close($ch);
            $return_data = json_decode($return, true);
            $view = new View();
            $view->assign('url', $return_data);
            $view->assign('domain', $domain);
            return $view->fetch('url_edit', [], ['__PUBLIC__' => '/public/static']);
        } else {
            $this->error('这不是你的域名');
        }
    }
}